package com.bottle.web;

import com.bottle.dto.UserDto;
import com.bottle.enity.User;
import com.bottle.enity.UserBridge;
import com.bottle.mode.ReJson;
import com.bottle.mode.UserInfo;
import com.bottle.service.UserService;
import com.bottle.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: chenyuncong
 * @Date: 2020/5/11 15:19
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    //所有用户的加密id
    @Autowired
    private ConcurrentHashMap<String, String> userIdCache;

    @Autowired
    private  ConcurrentHashMap<String,WebSocketServer> wsSessionMap;

    @PostMapping("login")
    public ReJson login(@RequestBody UserDto userDto){
        UserVo userVo= new UserVo();
        User user = userService.getUserByAccount(userDto.getAccount());
        if(user!=null){
//            userInfo.setUserId(user.getId());
//            Set<String> list = userService.getUserIdByEncryptionId(user.getEncrUserId());
//            userInfo.setEncryptionIds(list);
            userVo.setEncrUserId(user.getEncrUserId());
            userVo.setName(user.getName());
        }
        return new ReJson(userVo);
    }

    //新建用户
    @PostMapping("createUser")
    public ReJson createUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        return new ReJson(userService.saveUser(user));
    }

    /**
     * 好友添加
     * @param encrUserId 用户加密id
     * @param enrcFriendId 被加认的加密id
     * @return
     */
    @PostMapping("addFriend")
    public ReJson addFriend(String encrUserId,String enrcFriendId) {
        // 计算桥梁id 添加一条桥梁信息，状态是0 不通过的
        // 然后更新 双方的 缓存userInfo对象
        return null;
    }

    /**
     * 通过好友
     * @param receipt 回执只能使用一次
     * @param encryptedId 桥梁加密id
     * @param status 状态：0 为不通过 1位通过
     * @return
     */
    @PostMapping("pastFriend")
    public ReJson pastFriend(String receipt,String encryptedId,Integer status){
        //encryptedId
        return null;
    }

    //获取好友列表
    @PostMapping("getFriendList")
    public ReJson getFriendList(String encrUserId) {
        //用户加密id
        //然后通过桥梁获取好友信息
        if(userIdCache.containsKey(encrUserId)){
            String userId=userIdCache.get(encrUserId);
            List<UserBridge> list = userService.getEncryptionIdsByUserId(userId);
            return  new ReJson(list);
        }
        return new ReJson();
    }

    //删除好友
    @PostMapping("delFriend")
    public ReJson delFriend(String encrUserId,String enrcFriendId) {
        //将数据移动到一个删除表中去
        return null;
    }

    //单个聊天窗获取历史信息，
    @PostMapping
    public ReJson getSingleHistory() {
        //根据条件获取历史数据
        return null;
    }


    //创建聊天组
    @PostMapping("createCharGroup")
    public ReJson createCharGroup() {
        return null;
    }

    //获取群列表
    @PostMapping("getCharGroup")
    public ReJson getCharGroup() {
        return null;
    }



}
